<?php

namespace app\admin\controller;

use app\model\BannerModel;
use support\Request;
use support\Response;
use Exception;
use app\model\Util;

class BannerController
{
    /**
     * 获取banner列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        try {
            $current = (int)$request->get('current', 1);
            $pageSize = (int)$request->get('pageSize', 10);
            
            $query = BannerModel::query()
                ->orderBy('sort_by', 'desc')
                ->orderBy('id', 'desc');
                
            $total = $query->count();
            $list = $query->offset(($current - 1) * $pageSize)
                ->limit($pageSize)
                ->get();

            foreach ($list as &$item) {
                $item['image_url'] = Util::pathToUrl($item->image);
                $item['created_at'] = date('Y-m-d H:i:s', $item->created_at);
            }
                
            return json([
                'data' => [
                    'list' => $list,
                    'total' => $total
                ],
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 提交/创建banner
     * @param Request $request
     * @return Response
     */
    public function submit(Request $request): Response
    {
        try {
            $data = $request->post();
            
            if (empty($data['title']) || empty($data['image'])) {
                throw new Exception('标题和图片不能为空');
            }

            // 判断是新建还是编辑
            if (!empty($data['id'])) {
                $banner = BannerModel::find($data['id']);
                if (!$banner) {
                    throw new Exception('banner不存在');
                }
            } else {
                $banner = new BannerModel();
            }

            $banner->title = $data['title'];
            $banner->image = $data['image'];
            $banner->path = $data['path'] ?? '';
            $banner->sort_by = $data['sort_by'] ?? 0;
            $banner->updated_at = time();
            
            // 新建时设置创建时间
            if (empty($data['id'])) {
                $banner->created_at = time();
            }
            
            if (!$banner->save()) {
                throw new Exception(empty($data['id']) ? '添加失败' : '更新失败');
            }

            return json([
                'data' => $banner,
                'msg' => empty($data['id']) ? '添加成功' : '更新成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 删除banner
     * @param Request $request
     * @return Response
     */
    public function delete(Request $request): Response
    {
        try {
            $id = $request->post('id');
            
            if (empty($id)) {
                throw new Exception('参数错误');
            }

            $banner = BannerModel::find($id);
            if (!$banner) {
                throw new Exception('banner不存在');
            }

            if (!$banner->delete()) {
                throw new Exception('删除失败');
            }

            return json([
                'data' => null,
                'msg' => '删除成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }
}
